package bhbt.service.log;


import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import bhbt.model.Member;
import bhbt.model.log.LogEle;
import bhbt.model.log.LogMoney;
import bhbt.model.log.LogMoneyTi;
import bhbt.model.log.LogStore;
import bhbt.service.MemberService;
import bhbt.service.core.BaseService;
import bhbt.utils.ContextUtil;






@Service
public class LogMoneyTiService extends BaseService {
	
	@Resource
    private MemberService memberService;
	@Resource
    private LogMoneyTiService logMoneyTiService;
	@Resource
    private LogMoneyService logMoneyService;
	
	/**
	 * 保存
	 * @param member
	 */
	public void saveTi(LogMoneyTi logMoneyTi){
		
		//新增记录
		logMoneyTiService.saveOrUpdate(logMoneyTi);
		
		
		Member member = ContextUtil.getMember();
		member= memberService.getByKey(member.getId());
		
		
		Double num = logMoneyTi.getNum()+logMoneyTi.getFei();
		
		Map mapMember = new HashMap();
        mapMember.put("id",member.getId());
        mapMember.put("diyUpdate","money=money-"+num);
        //更新余额 
        memberService.update(mapMember);
        
         
        member= memberService.getByKey(member.getId());
        Double yu = member.getMoney();
        
        
        //新增记录
        LogMoney logMoney = new LogMoney();
        logMoney.setMemberId(member.getId());
        logMoney.setTitle("现金币提现，提现金额："+logMoneyTi.getNum()+",手续费："+logMoneyTi.getFei());
        logMoney.setState(88);
        logMoney.setTotalNum(num);
        logMoney.setNum(-num);
        logMoney.setYu(yu);
        logMoney.setDongNum(0d);
        logMoney.setFaDate(new Date());
        logMoneyService.saveOrUpdate(logMoney);
		 
		 
	}
	
	/**
	 * 取消
	 * @param member
	 */
	public void updateCancel(Integer id,String caifuNo){
		
		//更新状态
		Map mapLogMoneyTi = new HashMap();
		mapLogMoneyTi.put("id",id);
		mapLogMoneyTi.put("state",30);
		mapLogMoneyTi.put("commitDate",new Date());
		mapLogMoneyTi.put("caifuNo",caifuNo);
		
    	logMoneyTiService.update(mapLogMoneyTi);
    	
    	
    	
    	LogMoneyTi logMoneyTi = logMoneyTiService.getByKey(id); 
    	Member member= memberService.getByKey(logMoneyTi.getMemberId());
		
		
		Double num = logMoneyTi.getNum()+logMoneyTi.getFei();
		
		Map mapMember= new HashMap();
		mapMember.put("id",member.getId());
		mapMember.put("diyUpdate","money=money+"+num);
        //更新余额 
        memberService.update(mapMember);
        

        member= memberService.getByKey(member.getId());
        Double yu = member.getMoney();
        
        
        
        //新增记录
        LogMoney logMoney = new LogMoney();
        logMoney.setMemberId(member.getId());
        logMoney.setTitle("现金币提现取消，提现金额："+logMoneyTi.getNum()+",手续费："+logMoneyTi.getFei());
        logMoney.setState(88);
        logMoney.setTotalNum(num);
        logMoney.setNum(-num);
        logMoney.setYu(yu);
        logMoney.setDongNum(0d);
        logMoney.setFaDate(new Date());
        logMoneyService.saveOrUpdate(logMoney);
	}

}
